package com.yutian.endecryption.mock.service;

import com.yutian.endecryption.aes.util.SymmetricEncoder;
import com.yutian.endecryption.config.Config;
import com.yutian.endecryption.mock.mapper.ExamineeMapper;
import com.yutian.endecryption.mock.pojo.Examinee;

/**
 * 考生信息service
 * @author zhang peng yang ming
 * @create 2017-07-31 10:14
 */
public class ExamineeService extends BaseService<ExamineeMapper> {

    /**
     * 根据考生身份证证信息获取详情
     *
     * @param idNum
     * @return
     */
    public Examinee retrieveExaminee (String idNum) {
        Examinee examinee = mapper(ExamineeMapper.class).
                retrieveExaminee(SymmetricEncoder.AESEncode(Config.KEY, idNum));
        sessionClose();

        return examinee;
    }

    /**
     * 创建或更新考生信息
     *
     * @param e
     * @return
     */
    public int createExaminee (Examinee e) {
        int r = 0;
        Examinee examinee = retrieveExaminee(e.getSfzmhm());

        e.setSfzmhm(SymmetricEncoder.AESEncode(Config.KEY, e.getSfzmhm()));
        e.setXm(SymmetricEncoder.AESEncode(Config.KEY, e.getXm()));
        e.setKsy1(SymmetricEncoder.AESEncode(Config.KEY, e.getKsy1()));

        // 如果用户存在，那么直接更新数据库的数据
        if (examinee != null) {
            /*
             * 更新字段
             * yyrq, ykrq, kscc, ksrq, kscs, ksy1, zt, yycs, bcyykscs, kscx, ckyy
             */
            e.setId(examinee.getId());
            r = mapper(ExamineeMapper.class).updateExaminee(e);
            sessionClose();
        } else {
            r = mapper(ExamineeMapper.class).createExaminee(e);
            sessionClose();
        }

        return r;
    }
}
